# !/usr/bin/env python
# -*- coding: utf-8 -*-

import arrow
from dateutil import rrule
from dateutil.relativedelta import *
from dateutil.parser import *
import datetime

class DateUtils:
    DATE = '%Y-%m-%d'
    DATE_TIME = '%Y-%m-%d %H:%M:%S'
    TIME = '%H:%M:%S'
    YYYYMMDD = '%Y%m%d'

    @staticmethod
    def now():
        """
        获取今日日期
        :return:
        """
        return datetime.datetime.now()

    @staticmethod
    def today():
        """
        获取今日日期
        :return:
        """
        now = DateUtils.now()
        return datetime.datetime(now.year, now.month, now.day)

    @staticmethod
    def tomorrow():
        """
        获取明日日期
        :return:
        """
        now = DateUtils.now()
        return DateUtils.addDays(datetime.datetime(now.year, now.month, now.day), 1)
    
    @staticmethod
    def stock_daily_end():
        """
		获取股票日线数据采集结束时间
		:return:
		"""
        now = DateUtils.now()
        return DateUtils.addDays(datetime.datetime(now.year, now.month, now.day), -1 if now.hour < 15 else 0)
    
    @staticmethod
    def getDate(date):
        """
        获取今日日期
        :return:
        """
        return datetime.datetime(date.year, date.month, date.day)

    @staticmethod
    def addYears(date, years):
        """
        年加减
        :return:
        """
        return date + relativedelta(years=years)

    @staticmethod
    def addMonths(date, months):
        """
        月加减
        :return:
        """
        return date + relativedelta(months=months)

    @staticmethod
    def addWeeks(date, weeks):
        """
        周加减
        :return:
        """
        return date + datetime.timedelta(weeks=weeks)

    @staticmethod
    def addDays(date, days):
        """
        天加减
        :return:
        """
        return date + datetime.timedelta(days=days)

    @staticmethod
    def addHours(date, hours):
        """
        小时加减
        :return:
        """
        return date + datetime.timedelta(hours=hours)

    @staticmethod
    def addMinutes(date, minutes):
        """
        分钟加减
        :return:
        """
        return date + datetime.timedelta(minutes=minutes)

    @staticmethod
    def addSeconds(date, seconds):
        """
        秒加减
        :return:
        """
        return date + datetime.timedelta(seconds=seconds)

    @staticmethod
    def addMilliseconds(date, milliseconds):
        """
        毫秒加减
        :return:
        """
        return date + datetime.timedelta(milliseconds=milliseconds)

    @staticmethod
    def format(date, pattern=DATE_TIME):
        """
        格式化时间
        :param date:
        :param pattern:
        :return:
        """
        return date.strftime(pattern)

    @staticmethod
    def parse(dateStr):
        """
        时间字符串转时间
        :param dateStr:
        :return:
        """
        return parse(dateStr)

    @staticmethod
    def dateToTimestamp10(date):
        """
        时间转10位时间戳
        :param dateStr:
        :return:
        """
        return arrow.get(date, 'Asia/Shanghai').timestamp

    @staticmethod
    def dateToTimestamp13(date):
        """
        时间转13位时间戳
        :param dateStr:
        :return:
        """
        return int(arrow.get(date, 'Asia/Shanghai').float_timestamp * 1000)

    @staticmethod
    def timestamp10ToDate(timestamp):
        """
        10位时间戳转时间
        :param dateStr:
        :return:
        """
        return arrow.Arrow.fromtimestamp(timestamp, 'Asia/Shanghai').datetime.replace(tzinfo=None)

    @staticmethod
    def timestamp13ToDate(timestamp):
        """
        13位时间戳转时间
        :param dateStr:
        :return:
        """
        return arrow.Arrow.fromtimestamp(timestamp / 1000, 'Asia/Shanghai').datetime.replace(tzinfo=None)

    @staticmethod
    def diff(unit, start, util):
        """
        计算时间差
        :param dateStr:
        :return:
        """
        return rrule.rrule(unit, dtstart=start, until=util).count()


